package cn.edu.seu.ssny.service.impl;

import cn.edu.seu.ssny.dao.IDeviceDataDao;
import cn.edu.seu.ssny.dao.IDeviceDataStdDao;
import cn.edu.seu.ssny.dao.IGatewayDao;
import cn.edu.seu.ssny.dao.IProjectDao;
import cn.edu.seu.ssny.dao.support.IBaseDao;
import cn.edu.seu.ssny.entity.*;
import cn.edu.seu.ssny.service.IDeviceDataService;
import cn.edu.seu.ssny.service.IDeviceDataStdService;
import cn.edu.seu.ssny.service.support.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import cn.edu.seu.ssny.common.utils.DateUtils;

/**
 * 设备数据表
 */
@Service
public class DeviceDataServiceImpl extends BaseServiceImpl<DeviceData, Integer> implements IDeviceDataService {

    @Autowired
    private IDeviceDataDao deviceDataDao;

    @Autowired
    private IDeviceDataStdService deviceDataStdService;

    @Override
    public IBaseDao<DeviceData, Integer> getBaseDao() {
        return this.deviceDataDao;
    }

    @Override
    public Optional<DeviceData> findFirstByDeviceOrderByCreateTimeDesc(Device device) {
        return deviceDataDao.findFirstByDeviceOrderByCreateTimeDesc(device);
    }

    @Override
    public Optional<DeviceData> findFirstByDeviceAndCreateTimeGreaterThanEqualOrderByCreateTimeAsc(Device device, Date createTime) {
        return deviceDataDao.findFirstByDeviceAndCreateTimeGreaterThanEqualOrderByCreateTimeAsc(device, createTime);
    }

    @Override
    public Optional<DeviceData> findFirstByDeviceAndCreateTimeLessThanEqualOrderByCreateTimeDesc(Device device, Date createTime) {
        return deviceDataDao.findFirstByDeviceAndCreateTimeLessThanEqualOrderByCreateTimeDesc(device, createTime);
    }

    @Override
    public Optional<DeviceData> findFirstByDeviceAndCreateTimeBetweenOrderByCreateTimeAsc(Device device, Date start, Date end) {
        return deviceDataDao.findFirstByDeviceAndCreateTimeBetweenOrderByCreateTimeAsc(device, start, end);
    }

    @Override
    public List<DeviceData> findAllByDeviceAndCreateTimeBetweenOrderByCreateTimeAsc(Device device, Date start, Date end) {
        return deviceDataDao.findAllByDeviceAndCreateTimeBetweenOrderByCreateTimeAsc(device, start, end);
    }

    @Override
    public void saveOrModify(DeviceData deviceData, Device device) {
        if (deviceData.getId() != null) {
            DeviceData dbDeviceData = find(deviceData.getId());
            dbDeviceData.setModifiedData(deviceData.getModifiedData());
            dbDeviceData.setModifiedTime(new Date());
            dbDeviceData.setCreateTime(deviceData.getCreateTime());
            dbDeviceData.setIsModified(true);
            update(dbDeviceData);
        } else {
            deviceData.setIsModified(true);
            deviceData.setModifiedTime(new Date());
            deviceData.setDevice(device);
            save(deviceData);
        }
    }

    /*用于添加新的数据*/
    @Override
    public void newDataProcess(Device device, Date datetime, double data) {
        DeviceData deviceData = new DeviceData();
        deviceData.setDevice(device);
        deviceData.setIsModified(false);
        deviceData.setCreateTime(datetime);
        deviceData.setData(data);
        saveAndFlush(deviceData);
        deviceDataStdService.newDataProcess(deviceData);
    }

    @Override
    public Date getRoundTime(DeviceData deviceData) {
        Date rawDatetime = deviceData.getCreateTime();
        int periodMin = deviceData.getDevice().getPeriod() / 60;
        return DateUtils.getRoundTime(rawDatetime, periodMin);
    }
}